<template>
  <div class="viewContain">
    <el-form :model="listQuery" size="small" label-width="100px">
      <el-row>
        <!-- <el-col :xs="{ span: 24 }" :sm="{ span: 12 }" :md="{ span: 6 }" :xl="{ span: 6 }">
          <el-form-item label="地区名称 :" prop="name">
            <el-input
              v-model="listQuery.name"
              placeholder="请输入"
              clearable
              @keyup.enter.native="handleSearch"
            />
          </el-form-item>
        </el-col> -->
        <el-col :xs="{ span: 24 }" :sm="{ span: 12 }" :md="{ span: 12 }" :xl="{ span: 12 }">
          <el-form-item label-width="20px">
            <el-button size="mini" type="primary" @click="handleAdd">新增</el-button>
            <!-- <el-button size="mini" type="primary" @click="handleSearch">查询</el-button>
            <el-button size="mini" type="primary" plain @click="handleReload">重置</el-button> -->
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <Dialog
      :model-value="dailog.visible"
      :title="dailog.title"
      :visible="dailog.visible"
      :width="dailog.width"
      @handleCloseDialog="handleCloseDialog"
      @handleConfirm="handleConfirm"
    >
      <template #content>
        <el-form
          ref="formData"
          label-width="100px"
          :model="formData"
          :rules="rules"
          size="small"
        >
          <el-row>
            <el-form-item label="地区名称 :" prop="name">
              <el-input
                v-model="formData.name"
                clearable
                placeholder="请输入名称"
                style="max-width: 200px"
              />
            </el-form-item>
          </el-row>
        </el-form>
      </template>
    </Dialog>
    <el-table
      ref="table"
      v-loading="loading"
      fit
      stripe
      :data="tableData"
      border
      size="mini"
      max-height="400"
    >
      <el-table-column align="center" label="序号" type="index">
        <template slot-scope="scope">
          <span>{{ (listQuery.pageNum - 1) * listQuery.pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="name" align="center" label="地区名称" />
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button class="text-button" size="mini" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button class="text-button el-button--danger" size="mini" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      :total-count="totalCount"
      :page.sync="listQuery.pageNum"
      :limit.sync="listQuery.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
// import { deleteDeliveryType, deliveryList, addDeliveryType, updateDeliveryType } from '@/api/index'
import { addArea, deleteArea, updateArea, areaList } from '@/api/index'
import pagination from '@/components/pagination/index.vue'
import Dialog from '@/components/dailog/index.vue'

export default {
  components: {
    Dialog, pagination
  },
  data() {
    return {
      loading: false,
      totalCount: 0,
      options: [],
      addressList: [],
      deliveryList: [],
      listQuery: {
        pageNum: 1,
        pageSize: 10
      },
      tableData: [],
      rules: {
        name: [
          { required: true, message: '请输入地区名称', trigger: 'blur' }
        ]
      },
      formData: {
        id: '',
        name: ''
      },
      dailog: {
        // height: '',
        title: '合作快递新增',
        visible: false,
        visibleAssign: false,
        width: '400px'
      }
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    // 地区列表
    getList() {
      this.loading = true
      areaList(this.listQuery).then(res => {
        if (res.code === 200) {
          this.tableData = res.data.list
          this.totalCount = res.data.total
          this.loading = false
        } else {
          this.loading = false
        }
      })
    },

    // 新增
    handleAdd() {
      // alert('开发中,敬请期待~')
      this.dailog.title = '地区新增'
      this.dailog.visible = true
    },
    // 查询
    handleSearch() {
      this.getList()
    },
    // 删除
    handleDelete(id) {
      // alert('开发中,敬请期待~')
      this.$confirm('是否删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // const params = { ids: id }
        const ids = id
        deleteArea({}, ids).then(res => {
          console.log('res====删除结果', res)
          if (res.code === 200) {
            this.getList()
          }
        })
      })
    },
    handleReload() {
      alert('开发中,敬请期待~')
    },
    handleEdit(row) {
      console.log('row===', row)
      this.dailog.visible = true
      this.dailog.title = '编辑'
      this.$nextTick(() => {
        this.formData = JSON.parse(JSON.stringify(row))
      })
    },
    handleConfirm() {
      console.log('this.dailog.title===', this.dailog.title)
      this.$refs['formData'].validate((valid) => {
        if (valid) {
          this.dailog.visible = false
          if (this.dailog.title === '地区新增') {
            addArea(this.formData).then(res => {
              console.log('res===新增结果', res)
              if (res.code === 200) {
                this.getList()
                // this.$refs['formData'].resetFields()
              }
            })
          } else {
            updateArea(this.formData).then(res => {
              console.log('res===新增结果', res)
              if (res.code === 200) {
                this.getList()
                // this.$refs['formData'].resetFields()
              }
            })
          }
        }
      })
    },
    handleCloseDialog() {
      this.dailog.visible = false
      // this.$refs['formData'].resetFields()
      this.formData = {
        id: '',
        name: ''
      }
      console.log(123)
    }
  }
}
</script>

<style scoped>

</style>

